//
//  UIImageView+DownloadImage.m
//  UI22_多线程
//
//  Created by 曹曹 on 16/4/8.
//  Copyright © 2016年 曹曹. All rights reserved.
//

#import "UIImageView+DownloadImage.h"

@implementation UIImageView (DownloadImage)

- (void)asyncDownloadImageWithURL:(NSString *)urlString completionHandler:(void (^)(NSData *data, UIImage *image))block {
    // 子线程
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSURL *url = [NSURL URLWithString:urlString];
        NSData *data = [NSData dataWithContentsOfURL:url];
        UIImage *image = [UIImage imageWithData:data];
        // 回到主线程.
        dispatch_async(dispatch_get_main_queue(), ^{
            self.image = image;
            block(data, image);
        });
    });
}

@end
